package com.sec.android.app.myfiles.facade.cmd;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.activity.ChangePasswordsActivity;
import com.sec.android.app.myfiles.dialog.ConfirmLockUnlockDialog;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.operation.FileOperationArgs;
import com.sec.android.app.myfiles.operation.FileOperationFragment;
import com.sec.android.app.myfiles.operation.FileOperationFragment.PostOperation;
import com.sec.android.app.myfiles.operation.FileOperator.Operation;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.command.SimpleCommand;

public class LockUnlockCmd extends SimpleCommand implements PostOperation {
    private static final String mLockDlgTag = "lock_dialog";

    private static Context mContext;
    private static AbsMyFilesFragment mFragment;
    private static FileRecord mRecord;

    public enum LockProgress {
        SHOW_CONFIRM_DLG,
        INPUT_PASSWD,
        DO_PROC
    }

    @Override
    public void execute(INotification notification) {
        Object[] params = (Object[]) notification.getBody();

        LockProgress prog = (LockProgress) params[0];

        switch (prog) {
            case SHOW_CONFIRM_DLG:
                initValue(params);
                showConfirmDlg();
                break;
            case INPUT_PASSWD:
                inputPasswd(mFragment.getActivity());
                break;
            case DO_PROC:
                doLockUnlock();
                break;
        }

    }

    private void initValue(Object[] params) {
        mContext = (Context) params[1];
        mFragment = (AbsMyFilesFragment) params[2];
        mRecord = (FileRecord) params[3];
    }

    public void showConfirmDlg() {
        ConfirmLockUnlockDialog dlg = ConfirmLockUnlockDialog.getInstance(mContext, mFragment);

        if (dlg != null) {
            dlg.show(mFragment.getFragmentManager(), mLockDlgTag);
        } else {
            inputPasswd(mFragment.getActivity());
        }
    }

    public void inputPasswd(Activity activity) {
        Intent intent = new Intent(activity, ChangePasswordsActivity.class);

        int requestCode = R.id.menu_unlock;

        try {
            activity.startActivityForResult(intent, requestCode);
        } catch (ActivityNotFoundException e) {
            Log.e(this, "ActivityNotFoundException:" + e.toString());
        }
    }

    public void doLockUnlock() {
        Operation op = Operation.UNLOCK;

        FileOperationArgs args = new FileOperationArgs();
        args.mSrc = mRecord;

        mFragment.finishActionMode();

        FileOperationFragment fg = FileOperationFragment.createInstance(mContext, mFragment,
                mFragment.getFragmentManager(), op, args, this);
        fg.show();

    }

    @Override
    public void onResult(Operation opType, boolean bRet, Bundle extra) {
    }
}
